﻿<?php

include("class.base.php");
if (!$sess = sisse_logitud()) { header("Location: index.php"); die(); }

$xtabel = htmlspecialchars($_GET["t"], ENT_QUOTES);
$tabel1 = kriips($xtabel, 0);
$t_id = htmlspecialchars($_GET["id"], ENT_QUOTES);

if (!$minu_grupi_oigused = grupi_oigused($sess)) {
	header("Location: index.php"); die();
}
if ($minu_grupi_oigused < 2 and $xtabel != "lemmikud") {
	header("Location: index.php"); die();
}

if (in_array($xtabel, getSystemTables()) and $minu_grupi_oigused < 3 and $xtabel != "lemmikud") { die(); }

if ($_SESSION["ref"] == "") {
	$ref = getenv("HTTP_REFERER");
	if ($ref == "") { $_SESSION["ref"] = "index.php"; }
	else { $_SESSION["ref"] = $ref; }
}

$orig_aeg = array(
"audio_trakk" => "salvestusaeg",
"video_trakk" => "salvestusaeg",
"foto" => "pildistamisaeg",
"kasikiri" => "koostamise_aeg");

$pealkiri1 = "Kustuta ".$tabel1;
$sisu =<<<SISU
<form action="" method="POST">
	<table width="100%">
SISU;
$allTables = get_tables();
$publicTables = getPublicTables();
$nonSystemTables = getNonSystemTables();
$infoTables = getInfoTables();
if (in_array($xtabel, $allTables)) {
	$paring = mysql_query("SELECT * FROM ".$xtabel." WHERE id='".$t_id."'");
	if (mysql_num_rows($paring)) {
		
		//hakkame otsima kirjeid, mis kustutatava kirjega seostuvad
		$alamateArv = 0;
		$alamateNimekiri = array();
		foreach ($allTables as $tabel) {
			$alamad = mysql_query("SHOW COLUMNS FROM ".$tabel);
			while ($rida = mysql_fetch_array($alamad)) {
				if ($rida[0] == $xtabel) {
					$alamateArv = $alamateArv + mysql_num_rows(mysql_query("SELECT id FROM ".$tabel." WHERE ".$xtabel."='".$t_id."'"));
					array_push($alamateNimekiri, $tabel);
				}
			}
		}
		if (in_array("keel", get_columns($xtabel))) {
			$alamateArv = $alamateArv + mysql_num_rows(mysql_query("SELECT id FROM materjal_keel WHERE materjal='".$tabel."' AND materjal_id='".$t_id."'"));
			array_push($alamateNimekiri, "materjal_keel");
		}
		if (in_array($xtabel, $publicTables)) {
			$alamateArv = $alamateArv + mysql_num_rows(mysql_query("SELECT id FROM digitaliseerija WHERE materjal='".$xtabel."' AND materjal_id='".$t_id."'"));
			array_push($alamateNimekiri, "digitaliseerija");
			$alamateArv = $alamateArv + mysql_num_rows(mysql_query("SELECT id FROM roll WHERE materjal='".$xtabel."' AND materjal_id='".$t_id."'"));
			array_push($alamateNimekiri, "roll");
		}
		
		if (isset($_POST["kustuta"]) and ($xtabel != "sari" or $alamateArv == 0) and ($xtabel != "osaleja" or $alamateArv == 0)) {
			$rida = mysql_fetch_array($paring);
			if ($minu_grupi_oigused < 3 and $rida["lisaja"] != $sess["id"]) { $viga = $lang["pole_oigusi_kustutamiseks"]; }
			else {
				$ylemus = "";
				$ylemus_id = 0;
				$ylemad = mysql_query("SHOW COLUMNS FROM ".$xtabel);
				while ($ylemRida = mysql_fetch_array($ylemad)) {
					if (in_array($ylemRida[0], $allTables)) {
						$ylemus = $ylemRida[0];
						$ylemus_id = $rida[$ylemRida[0]];
						break;
					}
				}
				mysql_query("DELETE FROM ".$xtabel." WHERE id='".$t_id."'");
				mysql_query("DELETE FROM lemmikud WHERE tabel='".$xtabel."' AND vali='".$t_id."'");
				if ($xtabel != "lemmikud") {
					mysql_query("INSERT INTO logi SET ip='".$_SERVER["REMOTE_ADDR"]."', syndmus='<a href=\'naita.php?t=kasutaja&id=".$sess["id"]."\'>".$sess["email"]."</a> kustutas ".kriips($xtabel)." id ".$t_id." ning ".$alamateArv." alamkirjet', andmebaasi_lisatud=NOW()");
				}
				$teade = $lang["kustutatud"];
				if (!in_array($xtabel, $infoTables)) {
					foreach ($alamateNimekiri as $alamTabel) {
						if ($alamTabel == "digitaliseerija") {
							mysql_query("DELETE FROM digitaliseerija WHERE materjal='".$xtabel."' AND materjal_id='".$t_id."'");
						}
						elseif ($alamTabel == "roll") {
							mysql_query("DELETE FROM roll WHERE materjal='".$xtabel."' AND materjal_id='".$t_id."'");
						}
						elseif ($alamTabel == "materjal_keel") {
							mysql_query("DELETE FROM materjal_keel WHERE materjal='".$xtabel."' AND materjal_id='".$t_id."'");
						}
						else {
							mysql_query("DELETE FROM ".$alamTabel." WHERE ".$xtabel."='".$t_id."'");
						}
					}
				}
				if (!strstr($_SESSION["ref"], "naita.php") and !strstr($_SESSION["ref"], "muuda.php")) {
					$teade .= "<meta http-equiv=\"refresh\" content=\"1;url=".$_SESSION["ref"]."\" />";
				}
				elseif ($ylemus != "") {
					if (strstr($_SESSION["ref"], "naita.php")) {
						$teade .= "<meta http-equiv=\"refresh\" content=\"1;url=naita.php?t=".$ylemus."&id=".$ylemus_id."\" />";
					}
					elseif (strstr($_SESSION["ref"], "muuda.php")) {
						$teade .= "<meta http-equiv=\"refresh\" content=\"1;url=muuda.php?t=".$ylemus."&id=".$ylemus_id."\" />";
					}
				}
				$_SESSION["ref"] = "";
			}
		}
		else {
			$ref = getenv("HTTP_REFERER");
			if ($ref == "") { $ref = "index.php"; }
			if ($xtabel == "lemmikud"){
				$sisu .="
					<tr><td>".$lang["oled_kindel_et_soovid_oma_lemmiku_kustutada"]."</td></tr>
					<tr><td><input type='submit' name='kustuta' value='".$lang["jah"]."' /> <a href='".$ref."'>".$lang["tagasi"]."</a></td></tr>";
			}
			elseif ($xtabel == "sari" and $alamateArv != 0) {
				$sisu .="
					<tr><td>".str_replace("{NUMBER}", $alamateArv, $lang["sarja_ei_saa_kustutada"])."</td></tr>
					<tr><td><a href='".$ref."'>".$lang["tagasi"]."</a></td></tr>";
			}
			elseif ($xtabel == "osaleja" and $alamateArv != 0) {
				$sisu .="
					<tr><td>".str_replace("{NUMBER}", $alamateArv, $lang["osalejat_ei_saa_kustutada"])."</td></tr>
					<tr><td><a href='".$ref."'>".$lang["tagasi"]."</a></td></tr>";
			}
			else {
				$sisu .="
					<tr><td>".str_replace("{TABEL}", $tabel1, $lang["oled_kindel_et_soovid_kustutada"]);
				if ($alamateArv > 0 && !in_array($xtabel, $infoTables)) {
					$sisu .= str_replace("{NUMBER}", $alamateArv, " ".$lang["koos_sellega_kustutaksid_veel_seotud_kirjet"]);
				}
				$sisu .= "</td></tr>";
				if ($alamateArv > 0) {
					$sisu .= "
						<tr><td>".$lang["seotud_kirjed"].":
					";
					foreach ($alamateNimekiri as $alamTabel) {
						if ($alamTabel == "digitaliseerija" or $alamTabel == "roll" or $alamTabel == "materjal_keel") {
							$query = mysql_query("SELECT * FROM ".$alamTabel." WHERE materjal='".$xtabel."' AND materjal_id='".$t_id."'");
							$orig_materjal = mysql_fetch_array(mysql_query("SELECT ".$orig_aeg[$xtabel]." FROM ".$xtabel." WHERE id='".$t_id."'"));
						}
						else {
							$query = mysql_query("SELECT * FROM ".$alamTabel." WHERE ".$xtabel."='".$t_id."'");
						}
						if ($query && mysql_num_rows($query)) {
							$must_show_columns = get_must_show_columns($alamTabel);
							$sisu .= "<br /><br /><b>".kriips($alamTabel)."</b>:<br />";
							while ($row = mysql_fetch_array($query)) {
								if (in_array("vanus", $must_show_columns)) {
									$row2 = mysql_query("SELECT * FROM osaleja WHERE id='".$row["osaleja"]."'");
									if ($row2 && $row2 = mysql_fetch_array($row2)) {
										if (isset($row2["synniaasta"])) { $synniaasta = $row2["synniaasta"]; }
									}
								}
								$sisu .= "<a href='naita.php?t=".$alamTabel."&id=".$row["id"]."' target='_blank'>";
								foreach ($must_show_columns as $value) {
									if ($value == "kestus") {
										$sisu .= " ".pretty_length($row[$value]);
									}
									elseif ($value == "materjal") {
										if ($query2 = mysql_query("SELECT * FROM ".$row["materjal"]." WHERE id='".$row["materjal_id"]."'")) {
											$sisu = rtrim($sisu, " ");
											$sisu .= ": ".kriips($row["materjal"]);
											$row2 = mysql_fetch_array($query2);
											$must_show_columns2 = get_must_show_columns($row["materjal"]);
											foreach ($must_show_columns2 as $value2) {
												if ($value2 == "kestus") {
													$sisu .= " ".pretty_length($row2[$value2]);
												}
												else {
													$sisu .= " ".$row2[$value2];
												}
											}
										}
									}
									elseif (in_array($value, $nonSystemTables)) {
										if ($query2 = mysql_query("SELECT * FROM ".$value." WHERE id='".$row[$value]."'")) {
											$row2 = mysql_fetch_array($query2);
											$must_show_columns2 = get_must_show_columns($value);
											foreach ($must_show_columns2 as $value2) {
												if ($value2 == "kestus") {
													$sisu .= " ".pretty_length($row2[$value2]);
												}
												else {
													$sisu .= " ".$row2[$value2];
												}
											}
										}
									}
									elseif ($value == "vanus" && ($row["vanus"] || $row["vanus"] = vanus($synniaasta, $orig_materjal[$orig_aeg[$xtabel]]))) {
										$sisu .= " (".$row[$value].")";
									}
									else {
										$sisu .= " ".$row[$value];
									}
								}
								$sisu .= "</a>";
								$synniaasta = "";
								$sisu .= "<br />";
							}
						}
					}
					$sisu .= "</td></tr>";
				}
				$sisu .= "<tr><td><input type='submit' name='kustuta' value='".$lang["jah"]."' /> <a href='".$ref."'>".$lang["tagasi"]."</a></td></tr>";
			}
		}
	}
	else {
		$viga = $lang["tabelis_pole_sellist_kirjet_olemas"]." <meta http-equiv=\"refresh\" content=\"2;url=".$_SESSION["ref"]."\" />";
		$_SESSION["ref"] = "";
	}
}
else {
	$viga = $lang["tabelit_pole_olemas"]." <meta http-equiv=\"refresh\" content=\"2;url=".$_SESSION["ref"]."\" />";
	$_SESSION["ref"] = "";
}
$sisu .=<<<SISU
	</table>
</form>
SISU;
include("kujundus.php");
?>
